<?php
require_once("admin_global.php");    //加载后台全局调用类文件
$db->Get_user_shell_check($uid,$shell);   //后台登录控制
$db->Get_user_type_check($uid,$shell);    //用户权限控制

//查找出已有栏目并存储到数组中
$sql="SELECT * FROM category";
$query=$db->query($sql);
while($row=$db->fetch_array($query)){
    $data[]=$row;
}
//实例化一个树形类对象
$tree=new Tree($data);
//获取树形栏目结构
$gettree=$tree->get_tree(0,"<option value=\$id >\$spacer\$cateName</option>",0);
//获取栏目风格并存储到数组
$sql="SELECT * FROM catestyle WHERE isEnabled=1";
$query=$db->query($sql);
while($row=$db->fetch_array($query)){
    $styleResult[]=$row;
}
//判断用户是否提交了添加栏目的按钮
if(isset($_POST['addCate'])){
    //同一级栏目下不能有相同的栏目名称
    $catePID=$_POST['pid'];
    $cateName=trim($_POST['cateName']);
    $sql="SELECT * FROM category WHERE cateName='$cateName' AND catePID=$catePID";
    $query=$db->query($sql);
    $num=$db->db_num_rows($query);
    if($num<=0){
        //一级导航栏目不能设置为单页栏目
        $cateStyleID=$_POST['cateStyleID'];
        if(!($catePID==0 AND $cateStyleID==2)){
            //单页栏目下面不能创建子栏目
            $sql="SELECT * FROM category WHERE id=$catePID";
            $query=$db->query($sql);
            $result=$db->fetch_array($query);
            $parentStyleID=$result['cateStyleID'];
            if($parentStyleID!=2){
                $cateRemark=$_POST['cateRemark'];
                $cateNav=$_POST['cateNav'];
                $cateIndex=$_POST['cateIndex'];
                if(!empty($_POST['isShow'])){
                    $isShow=1;
                }else{
                    $isShow=0;
                }
                $cateCreateDate=date('Y-m-d',time());
                //准备写入数据库
                $sql="INSERT INTO category(id,catePID,cateName,isShow,cateStyleID,cateIndex,cateNavigation,cateCreateDate,cateRemark)  VALUES(null,$catePID,'$cateName',$isShow,$cateStyleID,$cateIndex,'$cateNav','$cateCreateDate','$cateRemark')";
                $query=$db->query($sql);
                if($query==true){
                    echo "<script>alert('栏目添加成功！');location.href='listCate.php';</script>";
                }else{
                    echo "<script>alert('栏目添加失败！');history.go(-1);</script>";
                }
            }else{
                echo "<script>alert('不能在单页栏目下创建子栏目！');history.go(-1);</script>";
            }
        }else{
            echo "<script>alert('一级导航栏目不能是单页栏目！');history.go(-1);</script>";
        }
    }else{
        echo "<script>alert('该栏目已存在！');history.go(-1);</script>";
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>添加栏目</title>
        <link rel="stylesheet" href="css/right.css" type="text/css" /> 
    </head>
    <script>
        window.onload=function(){
            var myform=document.getElementById('form1')
            var reg=new RegExp("^[0-9]*$");
            myform.onsubmit=function(){
                if(myform.cateName.value==""){
                    alert('请输入栏目名称！');
                    myform.cateName.focus();
                    return false;
                }
                if(myform.cateIndex.value==""){
                    alert('请输入栏目序号！');
                    myform.cateIndex.focus();
                    return false;
                }
                if(!reg.test(myform.cateIndex.value)){
                    alert('栏目序号必须是数字！');
                    myform.cateIndex.focus();
                    return false;
                }
                if(0>parseInt(myform.cateIndex.value)){
                    alert('栏目序号在0~99之间！');
                    myform.cateIndex.focus();
                    return false;
                }
                if(parseInt(myform.cateIndex.value)>99){
                    alert('栏目序号在0~99之间！');
                    myform.cateIndex.focus();
                    return false;
                }
            }
        }
    </script>
    <body>
        <div id="right_main">
            <div class="right_content">
                <div class="right_top_menu">
                    <h3>
                        您当前的位置：后台 > 栏目管理 > 添加栏目
                    </h3>
                </div>
                <div class="right_center_content">
                    <form id="form1" method="post" action="">
                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                            <tr>
                                <td valign="top" >
                                    <div class="txl_search"></div>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%"  class="sy_table">
                                        <tr>
                                            <th colspan="2" >添加栏目分类</th>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">所属栏目：</td>
                                            <td width="65%">
                                                <select name="pid">
                                                    <option value="0" >无（作为一级栏目）</option> 
                                                    <?php print_r($gettree); ?>
                                                </select>
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">栏目名称：</td>
                                            <td width="65%"><input type="text" name="cateName"  class="int_text" value="" size="40" /></td>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">栏目说明：</td>
                                            <td width="65%"><input type="text" name="cateRemark"  class="int_text" value="" size="40" />&nbsp;&nbsp;可以不填</td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">栏目风格：</td>
                                            <td width="65%">
                                                <select name="cateStyleID">
                                                   <?php foreach ($styleResult as $row){; ?>
                                                       <option value="<?php echo $row['cateStyleID']; ?>"><?php echo $row['cateStyleName']; ?></option>
                                                    <?php }; ?>
                                                </select> * 栏目风格是指栏目在前台页面显示时的外观样式，默认为文章列表样式，单页栏目请选择“单页内容”，教师风采等以图片为主的栏目请选择图片列表。
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">栏目位置：</td>
                                            <td width="65%">
                                                <select name="cateNav">
                                                    <option value="主导航" selected="selected">主导航</option> 
                                                    <option value="侧导航">侧导航</option> 
                                                </select>
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">栏目排序：</td>
                                            <td width="65%"><input type="text" name="cateIndex"  class="int_text" value="0" size="10" /> &nbsp;值为0-99，值越大，越靠前</td>
                                        </tr>   
                                        <tr>
                                            <td width="35%" align="right">是否显示：</td>
                                            <td width="65%"><input type="checkbox" name="isShow"  class="int_text"  checked="checked" value="6" size="40" /></td>
                                        </tr> 
                                        <tr>
                                            <td colspan="2" align="center" height='30'>
                                                <input type="submit" class="btn_ipt01" name="addCate" value=" 添加栏目 "/>
                                            </td>  
                                        </tr>
                                    </table>
                                </td>
                            </tr>   
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
